/*
 * @lc app=leetcode.cn id=1046 lang=typescript
 *
 * [1046] 最后一块石头的重量
 */

// @lc code=start
function lastStoneWeight(stones: number[]): number {
    const sorted = stones.sort((a, b) => a - b);
    while (sorted.length > 1) {
        const bigger = sorted.pop()!;
        const smaller = sorted.pop()!;
        const after = Math.abs(bigger - smaller);
        const index = sorted.findIndex((i) => {
            return after < i;
        });
        sorted.splice(index, 0, after);
    }
    return sorted[0] || 0;
}
// @lc code=end
